import os

import wx
import wx.lib.flatnotebook as fnb

from GCNCmd import Task
from MyPanel import *
from MyGrid import *
from MyNB import *


ID_PROTEIN_GRID = wx.NewId()
ID_ORTH_GRID = wx.NewId()
ID_RESULT_GRID = wx.NewId()
ID_NEW_MENU = wx.NewId()
ID_RUN_MENU = wx.NewId()

class GCNFrame(wx.Frame):
    def __init__(self, parent, task, draw):
        self.task = task
        self.draw = draw
        wx.Frame.__init__(self, parent, title='GCNBuilder', size=(800, 600))
        self.create_menu_bar()
        self.layout_items()

    def create_menu_bar(self):
        self._menu_bar = wx.MenuBar(wx.MB_DOCKABLE)
        self._task_menu = wx.Menu()
        self._edit_menu = wx.Menu()
        self._help_menu = wx.Menu()

        item = wx.MenuItem(self._task_menu, ID_NEW_MENU, 'New')
        self._task_menu.AppendItem(item)
        self.Bind(wx.EVT_MENU, self.OnNew, item)

        item = wx.MenuItem(self._task_menu, ID_RUN_MENU, 'Run')
        self._task_menu.AppendItem(item)
        self.Bind(wx.EVT_MENU, self.OnRun, item)

        item = wx.MenuItem(self._task_menu, wx.ID_ANY, 'Exit')
        self._task_menu.AppendItem(item)

        self._menu_bar.Append(self._task_menu, 'Task')
        self._menu_bar.Append(self._edit_menu, 'Edit')
        self._menu_bar.Append(self._help_menu, 'Help')

        self.SetMenuBar(self._menu_bar)


    def layout_items(self):
        book_style = fnb.FNB_NODRAG | fnb.FNB_NO_X_BUTTON

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(main_sizer)

        up_panel = wx.Panel(self, -1)

        up_sizer = wx.BoxSizer(wx.HORIZONTAL)
        up_panel.SetSizer(up_sizer)

        #self.setting_book = fnb.FlatNotebook(self, style=book_style)
        self.setting_book = SettingNB(self.task, self.draw, up_panel)

        #self.setting_book = InputSettingPanel(self.task, self)
        up_sizer.Add(self.setting_book, 2, wx.EXPAND)

        #spacer = wx.Panel(self, -1)
        #spacer.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE))
        #up_sizer.Add(spacer, 0, wx.ALL | wx.EXPAND)

        self.graph_panel = GraphPanel(self.task, self.draw, up_panel)
        up_sizer.Add(self.graph_panel, 5, wx.EXPAND)
        '''
        self.Freeze()

        self.input_setting_panel = InputSettingPanel(self.task, self.setting_book)
        self.setting_book.AddPage(self.input_setting_panel, 'Task setting')

        self.Thaw()
        '''
        main_sizer.Add(up_panel, 6, wx.EXPAND)

        #Add spacer
        spacer = wx.Panel(self, -1)
        spacer.SetBackgroundColour(wx.SystemSettings_GetColour(wx.SYS_COLOUR_3DFACE))
        main_sizer.Add(spacer, 0, wx.ALL | wx.EXPAND)

        self.data_book = fnb.FlatNotebook(self, style = book_style)
        main_sizer.Add(self.data_book, 3, wx.EXPAND)

        self.Freeze()

        self.protein_grid = ProteinGrid(self.task, self.data_book)
        self.orth_grid = OrthGrid(self.data_book)
        self.data_book.AddPage(self.protein_grid, 'Protein Data')
        self.data_book.AddPage(self.orth_grid, 'Ortholog Data',select=False)
        self.Thaw()


    def OnRun(self, e):
        if self.task.check():
            self.task.run()
            self.Freeze()
            self.result_grid = ResultGrid(self.task, self)
            self.data_book.AddPage(self.result_grid, 'Result')
            self.result_grid.update_data(self.task.pp_file)
            self.Thaw()

            #draw
        self.graph_panel.draw_graph()
        # test draw
        self.graph_panel.draw()


    def OnNew(self, e):
        self.create_menu_bar()
        self.layout_items()
        self.task = Task.Task()






#END